<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章新增</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <!-- 做日期模板 -->
    <link rel="stylesheet" href="./libs/layui/css/layui.css">
    <script src="./libs/layui/layui.js"></script>
    <!-- 富文本编辑器 -->
    <script src="./libs/tinymce_5.5.1/tinymce.min.js"></script>

    <!-- jq -->
    <script src="./libs/jquery/jquery.min.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章新增
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form" id="form">
                <div class="form-group">
                    <label for="inputTitle" class="col-sm-2 control-label">文章标题：</label>
                    <div class="col-sm-10">
                        <input type="text" name="title" class="form-control title" id="inputTitle" placeholder="文章标题文字">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCover" class="col-sm-2 control-label">文章封面：</label>
                    <div class="col-sm-10">
                        <img src="./images/2.jpg" class="article_cover" id="changeImg">
                        <input name="cover" type="file" id="inputCover" onchange="changeCoverImage()">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCategory" class="col-sm-2 control-label">文章类别：</label>
                    <div class="col-sm-4">
                        <select id="category" class="form-control category" name="categoryId">

                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">发布时间：</label>
                    <div class="col-sm-4">
                        <input id="articleDate" name='date' class="jeinput" type="text" placeholder="请选择发布日期" readonly>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章内容：</label>
                    <div class="col-sm-10">
                        <!-- 富文本编辑区域 -->
                        <textarea id="articleContent" placeholder="请输入文章内容"></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" id="release" class="btn btn-success btn-edit">发布</button>
                        <button type="submit" id="draft" class="btn btn-default btn-draft">存为草稿</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script src="./js/http.js"></script>
    <script>
        $(function () {
            // 获取文章分类
            getCategory();
            // 日期模板
            getData();
            // 富文本编辑器
            createTin();
            // 发布文章
            $('#release').click(function (e) {
                // 阻止表单默认提交行为
                e.preventDefault();
                saveArtical('已发布');
            })
            $('#draft').click(function (e) {
                e.preventDefault();
                saveArtical('草稿');
            })
        })
        // 获取文章类别
        function getCategory() {
            myAjax('GET', '/admin/category/list', {}, function (resData) {
                resData.data.forEach((item) => {
                    let art_categoryHtml = `<option value="${item.id}">${item.name}</option>`
                    $('#category').append(art_categoryHtml);
                })
            })
        }
        // 日期模板
        function getData() {
            layui.use('laydate', function () {
                var laydate = layui.laydate;

                //执行一个laydate实例
                laydate.render({
                    elem: '#articleDate' //指定元素
                });
            });
        }
        // 生成富文本编辑器
        function createTin() {
            tinymce.init({
                selector: '#articleContent', //容器，可使用css选择器
                language: 'zh_CN', //调用放在langs文件夹内的语言包
            });
        }
        // 头像本地生成可本地访问的路径blob
        function changeCoverImage() {
            let fileImg = document.getElementById('inputCover').files[0];
            let coverUrl = URL.createObjectURL(fileImg);
            $('#changeImg').attr('src', coverUrl);
        }
        // 文章发布和草稿功能
        function saveArtical(type) {
            let formObj = {
                title: $('#inputTitle').val(),
                cover: document.querySelector('#inputCover').files[0],
                categoryId: $('#category').val(),
                date: $('#articleDate').val(),
                content: tinyMCE.editors['articleContent'].getContent(),
                state: type
            }
            let formData = new FormData();
            for (let key in formObj) {
                formData.append(key, formObj[key]);
            }
            myAjax('POST', '/admin/article/publish', formData, function (resData) {
                console.log('文章发布', resData);
                if (resData.code !== 200) {
                    alert(resData.msg);
                    return;
                }
                // $('.level02 li', window.parent.document).eq(0).addClass('active').siblings().removeClass('active');
                window.location = './article_list.html';
            })
        }

    </script>
</body>

</html>